home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / mod_auth_dbm.c < prev    next >
Text File  |  1995-12-04  |  8KB  |  253 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * http_auth: authentication
  57.  * 
  58.  * Rob McCool & Brian Behlendorf.
  59.  * 
  60.  * Adapted to Shambhala by rst.
  61.  */
  62.  
  63. #include "httpd.h"
  64. #include "http_config.h"
  65. #include "http_core.h"
  66. #include "http_log.h"
  67. #include "http_protocol.h"
  68. #include <ndbm.h>
  69.  
  70. typedef struct  {
  71.  
  72.     char *auth_dbmpwfile;
  73.     char *auth_dbmgrpfile;
  74.  
  75. } dbm_auth_config_rec;
  76.  
  77. void *create_dbm_auth_dir_config (pool *p, char *d)
  78. {
  79.     return pcalloc (p, sizeof(dbm_auth_config_rec));
  80. }
  81.  
  82. command_rec dbm_auth_cmds[] = {
  83. { "AuthDBMUserFile", set_string_slot,
  84.     (void*)XtOffsetOf(dbm_auth_config_rec, auth_dbmpwfile),
  85.     OR_AUTHCFG, TAKE1, NULL },
  86. { "AuthDBMGroupFile", set_string_slot,
  87.     (void*)XtOffsetOf(dbm_auth_config_rec, auth_dbmgrpfile),
  88.     OR_AUTHCFG, TAKE1, NULL },
  89. { NULL }
  90. };
  91.  
  92. module dbm_auth_module;
  93.  
  94. char *get_dbm_pw(request_rec *r, char *user, char *auth_dbmpwfile) {
  95.     DBM *f; 
  96.     datum d, q; 
  97.     char *pw = NULL;
  98.  
  99.     q.dptr = user; 
  100.     q.dsize = strlen(q.dptr); 
  101.     
  102.     if(!(f=dbm_open(auth_dbmpwfile,O_RDONLY,0664))) {
  103.         log_reason ("could not open dbm auth file", auth_dbmpwfile, r);
  104.     return NULL;
  105.     }
  106.  
  107.     d = dbm_fetch(f, q);
  108.  
  109.     if (d.dptr) {
  110.         pw = palloc (r->pool, d.dsize + 1);
  111.     strncpy(pw,d.dptr,d.dsize);
  112.     pw[d.dsize] = '\0';         /* Terminate the string */
  113.     }
  114.  
  115.     dbm_close(f);
  116.     return pw; 
  117. }
  118.  
  119. /* We do something strange with the group file.  If the group file
  120.  * contains any : we assume the format is
  121.  *      key=username value=":"groupname [":"anything here is ignored]
  122.  * otherwise we now (0.8.14+) assume that the format is
  123.  *      key=username value=groupname
  124.  * The first allows the password and group files to be the same 
  125.  * physical DBM file;   key=username value=password":"groupname[":"anything]
  126.  *
  127.  * mark@telescope.org, 22Sep95
  128.  */
  129.  
  130. char  *get_dbm_grp(request_rec *r, char *user, char *auth_dbmgrpfile) {
  131.     char *grp_data = get_dbm_pw (r, user, auth_dbmgrpfile);
  132.     char *grp_colon; char *grp_colon2;
  133.  
  134.     if (grp_data == NULL) return NULL;
  135.     
  136.     if ((grp_colon = strchr(grp_data, ':'))!=NULL) {
  137.         grp_colon2 = strchr(++grp_colon, ':');
  138.         if (grp_colon2) *grp_colon2='\0';
  139.         return grp_colon;
  140.     }
  141.     return grp_data;
  142. }
  143.  
  144. int dbm_authenticate_basic_user (request_rec *r)
  145. {
  146.     dbm_auth_config_rec *sec =
  147.       (dbm_auth_config_rec *)get_module_config (r->per_dir_config,
  148.                         &dbm_auth_module);
  149.     conn_rec *c = r->connection;
  150.     char *sent_pw, *real_pw, *colon_pw;
  151.     char errstr[MAX_STRING_LEN];
  152.     int res;
  153.     
  154.     if ((res = get_basic_auth_pw (r, &sent_pw)))
  155.         return res;
  156.     
  157.     if(!sec->auth_dbmpwfile)
  158.         return DECLINED;
  159.     
  160.     if(!(real_pw = get_dbm_pw(r, c->user, sec->auth_dbmpwfile))) {
  161.         sprintf(errstr,"DBM user %s not found", c->user);
  162.     log_reason (errstr, r->filename, r);
  163.     note_basic_auth_failure (r);
  164.     return AUTH_REQUIRED;
  165.     }    
  166.     /* Password is up to first : if exists */
  167.     colon_pw = strchr(real_pw,':');
  168.     if (colon_pw) *colon_pw='\0';   
  169.     /* anyone know where the prototype for crypt is? */
  170.     if(strcmp(real_pw,(char *)crypt(sent_pw,real_pw))) {
  171.         sprintf(errstr,"user %s: password mismatch",c->user);
  172.     log_reason (errstr, r->uri, r);
  173.     note_basic_auth_failure (r);
  174.     return AUTH_REQUIRED;
  175.     }
  176.     return OK;
  177. }
  178.     
  179. /* Checking ID */
  180.     
  181. int dbm_check_auth(request_rec *r) {
  182.     dbm_auth_config_rec *sec =
  183.       (dbm_auth_config_rec *)get_module_config (r->per_dir_config,
  184.                         &dbm_auth_module);
  185.     char *user = r->connection->user;
  186.     int m = r->method_number;
  187.     char errstr[MAX_STRING_LEN];
  188.     
  189.     array_header *reqs_arr = requires (r);
  190.     require_line *reqs = reqs_arr ? (require_line *)reqs_arr->elts : NULL;
  191.  
  192.     register int x;
  193.     char *t, *w;
  194.  
  195.     if (!sec->auth_dbmgrpfile) return DECLINED;
  196.     if (!reqs_arr) return DECLINED;
  197.     
  198.     for(x=0; x < reqs_arr->nelts; x++) {
  199.       
  200.     if (! (reqs[x].method_mask & (1 << m))) continue;
  201.     
  202.         t = reqs[x].requirement;
  203.         w = getword(r->pool, &t, ' ');
  204.     
  205.         if(!strcmp(w,"group") && sec->auth_dbmgrpfile) {
  206.            char *orig_groups,*groups,*v;
  207.  
  208.            if (!(groups = get_dbm_grp(r, user, sec->auth_dbmgrpfile))) {
  209.                sprintf(errstr,"user %s not in DBM group file %s",
  210.                user, sec->auth_dbmgrpfile);
  211.            log_reason (errstr, r->filename, r);
  212.            note_basic_auth_failure (r);
  213.            return AUTH_REQUIRED;
  214.            }
  215.            orig_groups = groups;
  216.            while(t[0]) {
  217.                w = getword(r->pool, &t, ' ');
  218.                groups = orig_groups;
  219.                while(groups[0]) {
  220.                    v = getword(r->pool, &groups,',');
  221.                    if(!strcmp(v,w))
  222.                        return OK;
  223.                }
  224.            }
  225.            sprintf(errstr,"user %s not in right group",user);
  226.        log_reason (errstr, r->filename, r);
  227.            note_basic_auth_failure(r);
  228.        return AUTH_REQUIRED;
  229.        }
  230.     }
  231.     
  232.     return DECLINED;
  233. }
  234.  
  235.  
  236. module dbm_auth_module = {
  237.    STANDARD_MODULE_STUFF,
  238.    NULL,            /* initializer */
  239.    create_dbm_auth_dir_config,    /* dir config creater */
  240.    NULL,            /* dir merger --- default is to override */
  241.    NULL,            /* server config */
  242.    NULL,            /* merge server config */
  243.    dbm_auth_cmds,        /* command table */
  244.    NULL,            /* handlers */
  245.    NULL,            /* filename translation */
  246.    dbm_authenticate_basic_user,    /* check_user_id */
  247.    dbm_check_auth,        /* check auth */
  248.    NULL,            /* check access */
  249.    NULL,            /* type_checker */
  250.    NULL,            /* fixups */
  251.    NULL                /* logger */
  252. };
  253.